#!/usr/bin/perl -w

use Test::More tests =>  2;
# =============================================================
# Cargamos el modulo
# ==============================================================
use_ok('Bench::Test');

# =============================================================
# Establecemos los parametros adecuados
# =============================================================
SKIP: {
   my $host = $ENV{BENCH_TEST_REMOTE_HOST}
      or skip('because you must set enviroment var BENCH_TEST_REMOTE_HOST', 1);

   my @TESTS = Bench::Test::get_tests_from_file('examples/mm/TESTS');

   my @TABLE         = (
      DIM   => sub {
                  my $output = shift;
                  $output =~ /primera\s*matriz:\s*(.*)\n.*?:\s*(.*)\n.*?:\s*(.*)\n/;
                  return "$1:$2:$3";
               },
      RES   => 'Result:\s+(.*)\n',
      TIME  => 'Time of product:\s*(.*)\n',
   );


   my @VISUAL   = qw(TEST NAME DIM TIME);
   my @SELECTED = qw(LOCAL REMOTE);

   sub Bench::Test::Entry::is_OK { return 1; }

   my $MACHINE = GRID::Machine->new(
      host       => $host,
      sendstdout => 1,
      uses       => [ 'File::Temp' ],
   );

   my $tempdir = $MACHINE->eval(q{
      my $tempdir = File::Temp::tempdir( CLEANUP => 1 );
      chdir($tempdir);
      return $tempdir;
   })->result;

   my %EXPERIMENTS = ( 
      'LOCAL' => Bench::Test::Local->new  (
                     WORKDIR   => 'examples/mm',
                     EXECUTABLE => './mm',
                  ),
      'REMOTE' => Bench::Test::Remote->new (
                     MACHINE    => $MACHINE,
                     WORKDIR    => "$tempdir/mm",
                     EXECUTABLE => 'mm',
                     PREAMBLE   => sub {
                                     my $machine = shift;
                                     $machine->put(['examples/mm/mm.tar.gz']);
                                     $machine->system('tar xfz mm.tar.gz');
                                     $machine->chdir('mm');
                                     $machine->system('make');
                                   },
                     POSTAMBLE  => sub {
                                     my $machine = shift;
                                     $machine->chdir('..'); 
                                     $machine->system('rm -rf mm');
                                     $machine->system('rm mm.tar.gz');
                                   },
               ),
   );


   my $bt = Bench::Test->new(
      tests       => \@TESTS,
      table       => \@TABLE,
      visual      => \@VISUAL,
      experiments => \%EXPERIMENTS,
      selected    => \@SELECTED,
   );

   ok($bt->make_tests, 'Executing "Bench::Test::make_test" using remote machine');
}
